#ifndef QSERIALCONTROL_H
#define QSERIALCONTROL_H

#include <QObject>
#include <QtSerialPort/QSerialPort>
#include <QSerialPortInfo>
#include <QMutex>
#include <QThread>
#include "SerialBase.h"

#define TIME_OUT_MS 3000
#define MainSerialPortOneFrameSize 4

class QSerialControl: public QObject, public SerialBase
{
    Q_OBJECT
public:
    explicit QSerialControl();
    ~QSerialControl();
    virtual QStringList ScanSerial() override;
    ///
    virtual std::string SendUntilRec(const char *sendBuf,int msecs = 3000) override;
    ///
    virtual bool GetOpened() override;


    ///
    virtual int CloseSerial() override;

    ///
    virtual void SendBuff(const char *sendBuf) override;


    virtual void OpenPort(const char *port,int bau) override;//Open

    virtual bool ReadBuff() override;

signals:
    void to_start();
    void to_stop();


private:

    void creatService();
    QSerialPort *m_serialPort;
    QThread     *m_thread;

    QByteArray buff;
    QString port;
    int bau;
    bool isServiceRunning;


    void processJson(QByteArray);

    void InitPort();


protected slots:
    void receiveInfo();
    void startService();
};

#endif // QSERIALCONTROL_H
